package com.zone.vehicle.superApps.view.appmanager.adapter;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatButton;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.tamsiree.rxui.view.RxProgressBar;
import com.zone.sdk.module.appstore.entity.AppEntity;
import com.zone.vehicle.superApps.R;
import com.zone.vehicle.superApps.databinding.FragmentAppManagerItemBinding;

import org.apache.commons.lang3.ObjectUtils;
import org.jetbrains.annotations.NotNull;

import java.util.List;

public class AppManagerAdapterAdapter extends RecyclerView.Adapter<AppManagerAdapterAdapter.ViewHolder> {

    private static final String TAG = AppManagerAdapterAdapter.class.getSimpleName();

    public void setData(List<AppEntity> data) {
        this.data = data;
    }

    private List<AppEntity> data;

    private FragmentAppManagerItemBinding binding;

    public AppManagerAdapterAdapter(List<AppEntity> data) {
        this.data = data;
    }

    @NotNull
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        binding = FragmentAppManagerItemBinding.inflate(LayoutInflater.from(parent.getContext()));
        return new ViewHolder(binding.getRoot());
    }

    @Override
    public void onBindViewHolder(@NotNull ViewHolder holder, int position) {
        if (ObjectUtils.isEmpty(data)){
            return;
        }
        AppEntity appEntity = data.get(position);
        binding.name.setText(appEntity.getName());
        //设置图片大小
        RoundedCorners roundedCorners = new RoundedCorners(50);
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners)
                .placeholder(new ColorDrawable(Color.GRAY))//设置占位图
//                    .error(R.mipmap.logo_app)//设置异常图片
                .override(300, 300);//降低内存消耗
        Glide.with(holder.itemView.getContext())
                .asBitmap()
                .load(appEntity.getIcon())
                .apply(options)
                .into(holder.icon);
        holder.btn_activity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.btn_activity.setVisibility(View.INVISIBLE);
                holder.download_progress.setProgress(holder.download_progress.getProgress()+2);
            }
        });
        holder.download_progress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.btn_activity.setVisibility(View.VISIBLE);
                holder.download_progress.setProgress(holder.download_progress.getProgress()+2);
            }
        });
//        binding.time.setText();
//        AppEntity app = data.get(position);
//        //设置图片大小
//        RoundedCorners roundedCorners = new RoundedCorners(50);
//        //扩展功能
//        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners)
//                .placeholder(new ColorDrawable(Color.GRAY))//设置占位图
////                    .error(R.mipmap.logo_app)//设置异常图片
//                .override(300, 300);//降低内存消耗
//        if (ObjectUtils.isNotEmpty(app)){
//
//            Glide.with(holder.itemView.getContext())
//                    .asBitmap()
//                    .load(R.drawable.app_pic_demo)
//                    .apply(options)
//                    .into(holder.pic1);
//            holder.itemView.findViewById(R.id.item1).setTag(app);
////            Glide.with(holder.itemView.getContext()).load(app1.getIcon()).into(holder.icon1);
////            holder.pic1.setImageResource(R.drawable.app_pic_demo);
//            holder.name1.setText(app.getName());
////            holder.star1.setImageResource(getStarId(app1.getStar()));
//            holder.score1.setText(app.getScore());
//            if (StringUtils.isNotEmpty(app.getLabels())){
//                String[] tags = app.getLabels().split(",");
//                holder.tag11.setText(StringUtils.isNotBlank(tags[0])?tags[0]:"");
//                holder.tag12.setText(StringUtils.isNotBlank(tags[1])?tags[1]:"");
//                holder.tag13.setText(StringUtils.isNotBlank(tags[2])?tags[2]:"");
//            }
//            holder.description1.setText(app.getOneWord());
//
//        }

    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    private int getStarId(int star){
        switch (star){
            case 1:
                return R.mipmap.icon_star;
            case 2:
                return R.mipmap.icon_star;
            case 3:
                return R.mipmap.icon_star;
            case 4:
                return R.mipmap.icon_star;
            case 5:
                return R.mipmap.icon_star;
            default:
                return R.mipmap.icon_star;
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        private final ImageView icon;
        private final TextView name;
        private final RxProgressBar download_progress;
        private final AppCompatButton btn_activity;
//        private final TextView tag11;
//        private final TextView tag12;
//        private final TextView tag13;
//        private final TextView description1;
//
//        private final ImageView pic2;
//        private final TextView name2;
//        private final ImageView star2;
//        private final TextView score2;
//        private final TextView tag21;
//        private final TextView tag22;
//        private final TextView tag23;
//        private final TextView description2;


        public ViewHolder(View itemView) {
            super(itemView);
            icon = itemView.findViewById(R.id.icon);
            name = itemView.findViewById(R.id.name);
            download_progress = itemView.findViewById(R.id.download_progress);
            btn_activity = itemView.findViewById(R.id.btn_activity);
//            tag11 = itemView.findViewById(R.id.tag11);
//            tag12 = itemView.findViewById(R.id.tag12);
//            tag13 = itemView.findViewById(R.id.tag13);
//            description1 = itemView.findViewById(R.id.description1);
//
//            pic2 = itemView.findViewById(R.id.pic2);
//            name2 = itemView.findViewById(R.id.name2);
//            star2 = itemView.findViewById(R.id.star2);
//            score2 = itemView.findViewById(R.id.score2);
//            tag21 = itemView.findViewById(R.id.tag21);
//            tag22 = itemView.findViewById(R.id.tag22);
//            tag23 = itemView.findViewById(R.id.tag23);
//            description2 = itemView.findViewById(R.id.description2);
        }
    }



}
